package twoRegInstructions;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import main.IMIPSInstruction;


public abstract class MIPSTwoRegInstruction implements IMIPSInstruction {
	String myRegisterOne;
	String myRegisterTwo;
	int myImmediateValue;
	String myImmediateValueString;
	String myName;
	protected static Properties myRecognizedRegisters = new Properties();

	public MIPSTwoRegInstruction(String input) throws FileNotFoundException,
			IOException {
		myRecognizedRegisters.load(new FileInputStream(new File(
				"recognizedRegisters.properties")));
		myRegisterOne = myRecognizedRegisters.getProperty(input.substring(11,
				16));
		myRegisterTwo = myRecognizedRegisters.getProperty(input
				.substring(6, 11));
		
		String immediateValueString = input.substring(16, 32);
		
		myImmediateValue = Integer.parseInt(immediateValueString,2);
		byte[] b = new byte[4];
        for (int k=0; k<4; k++) 
        {
            int offset = (b.length-1-k)*8;
            b[k] = (byte)((myImmediateValue >>> offset)&0xFF);
        }
		StringBuffer sb = new StringBuffer( b.length*2 );
	    char[] hexChar = 
	    	{'0' , '1' , '2' , '3' ,
	    	'4' , '5' , '6' , '7' ,
	    	'8' , '9' , 'a' , 'b' ,
	    	'c' , 'd' , 'e' , 'f'};
	    for ( int k=0; k<b.length; k++ )
	    {
	    sb.append( hexChar [( b[k] & 0xf0 ) >>> 4] );
	    sb.append( hexChar [b[k] & 0x0f] );
	    }
	    String hexstring =  sb.toString();
	    myImmediateValueString = "0x" + hexstring.substring(hexstring.length() - 4, hexstring.length());
	
	}
	public MIPSTwoRegInstruction(){
		
	}
	
	public String toString(){
		return myName + " " + myRegisterOne +", " + myRegisterTwo + ", " + myImmediateValueString;
	}
}
